<?php

class Solution
{

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return Integer[]
     */
    function topKFrequent($nums, $k)
    {
        $map = new BinarySearchTreeMap();
        foreach ($nums as $key) {
            $value = $map->get($key);
            if ($value != null) {
                $map->set($key, $value + 1);
            } else {
                $map->add($key, 1);
            }
        }
        $minHeap = new MinHeap();
        $map->traverseMinHeap($minHeap, $k);

        $reArr = [];
        while (!$minHeap->isEmpty()) {
            $arr = $minHeap->getMin();
            $reArr[] = $arr["value"];
        }
        return $reArr;
    }
}
